package com.example.demo.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.example.demo.model.MsgClient;
import com.example.demo.model.MsgClientGroup;
import com.example.demo.util.ExcelExportUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author LIN
 * @date 2022-09-03 17:43
 */
@RequestMapping(value = "/easypoi")
@RestController
public class EasypoiController {

    @RequestMapping(value = "/export")
    public void bigDataExport(HttpServletResponse response) throws Exception {

        List<MsgClient> list = new ArrayList<MsgClient>();
        Workbook workbook = null;
        Date start = new Date();
        ExportParams params = new ExportParams("大数据测试", "测试");
        for (int i = 0; i < 1000000; i++) {  //一百万数据量
            MsgClient client = new MsgClient();
            client.setBirthday(new Date());
            client.setClientName("小明" + i);
            client.setClientPhone("18797" + i);
            client.setCreateBy("JueYue");
            client.setId("1" + i);
            client.setRemark("测试" + i);
            MsgClientGroup group = new MsgClientGroup();
            group.setGroupName("测试" + i);
            client.setGroup(group);
            list.add(client);
            if(list.size() == 10000){

                workbook = ExcelExportUtil.exportBigExcel(params, MsgClient.class, list);
                list.clear();
            }
        }
        ExcelExportUtil.closeExportBigExcel();
        System.out.println(System.currentTimeMillis() - start.getTime());
        File savefile = new File("excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream("excel/ExcelExportBigData.bigDataExport.xlsx");
        workbook.write(fos);
        System.out.println(fos.getChannel().size()/1024/1024);
        fos.close();
        Thread.sleep(10000);
        response.sendRedirect("/excel/ExcelExportBigData.bigDataExport.xlsx");
    }
    public static void main(String[] args) throws Exception {

    }
}
